package com.zznj.xzjc.activity.index.collect;

import android.content.Context;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.zznj.xzjc.R;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.fragment.index.AjcxDsjjsFragment;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.DictionaryBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.view.StatusBarUtil;
import com.zznj.xzjc.widget.FragmentAda;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 描述：线索发现
 */
public class ActivityMyCollectMain extends BaseActivity {
    @BindView(R.id.backImage)
    View backImage;
    @BindView(R.id.stl_main)
    SlidingTabLayout stlMain;
    @BindView(R.id.vp)
    ViewPager vp;

    Unbinder unbinder;

    @BindView(R.id.titleLinear)
    LinearLayout titleLinear;
    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.searchBtn)
    ImageView searchBtn;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_ajcx_dsjjs_layout;
    }
    private LoginBean user;
    private int selectIndex = 0;
    private String key;

    @Override
    public void initView() {
        selectIndex = getIntent().getExtras().getInt("index");
        key = getIntent().getExtras().getString("key");
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        searchEdit.setText(key);
        setTitle("案源库");
        init();
    }

    public String getKey(){
        return searchEdit.getText().toString();
    }

    private String[] mTitle = {"全部", "线索", "知识库", "工作信息", "案源"};
    private String[] mValue = {"","clue","knowledgeLib", "notice", "case"};
    private ArrayList<Fragment> arrayList = null;

    private void init() {

        /***
         * 监听输入框是否点击了搜索
         */
        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//关闭软键盘
                    ((AjcxDsjjsFragment) arrayList.get(vp.getCurrentItem())).search();
                    return true;
                }
                return false;
            }
        });

        arrayList = new ArrayList<>();
        for (int i = 0; i < mTitle.length; i++) {
//            arrayList.add(AjcxDsjjsFragment.getInstance(mValue[i]));
        }

        stlMain.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        vp.setAdapter(new FragmentAda(getSupportFragmentManager(), arrayList, null));
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        stlMain.setViewPager(vp, mTitle);


        stlMain.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        vp.setCurrentItem(selectIndex);
    }

    private List<DictionaryBean> fromList;

    @Override
    protected void initData() {
        titleLinear.setPadding(0, StatusBarUtil.getStatusBarHeight(mContext), 0, 0);
    }

    @OnClick({R.id.backImage,R.id.searchBtn})
    public void onViewClicked(View view) {
        if(view.getId() == R.id.backImage){
            finish();
        }else if (view.getId() == R.id.searchBtn) {
            ((AjcxDsjjsFragment) arrayList.get(vp.getCurrentItem())).search();
        }
    }
}
